package com.omkarpatil.learningjpa2.mapping.manytomany.uni;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

@Entity(name = "MtmUniTag")
@Table(name = "mtm_uni_Tag")
public class Tag implements Serializable {

	private int id;
	private String name;
	private Set<Post> posts = new HashSet<Post>();
	
	public Tag() {}
	
	public Tag(String name) {
		this.name = name;
	}

	@Id
	@GeneratedValue
	public int getId() {
		return id;
	}

	private void setId(int id) {
		this.id = id;
	}

	@Column(nullable = false, length = 30)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@ManyToMany
	@JoinTable(name = "mtm_uni_post_tag",
		joinColumns = @JoinColumn(name = "tag_id"),
		inverseJoinColumns = @JoinColumn(name = "post_id")
	)
	public Set<Post> getPosts() {
		return posts;
	}

	private void setPosts(Set<Post> posts) {
		this.posts = posts;
	}
	
	public void addPost(Post post) {
		this.getPosts().add(post);
	}
	
	public void removePost(Post post) {
		this.getPosts().remove(post);
	}
}
